using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Compiled;

namespace OR.Lazarus.Library.DataTier.Compiled.Lazarus
{
	/// <summary>
	/// Defines the parameters within a method.
	/// </summary>
   public class ParameterDetail : EntityBase
   {
		protected System.String mName;
		protected string mDBName;
		protected string mType;
		protected object mSize;
		protected object mScale;
		protected object mRequired;
		protected string mDirection;
		protected string mDescription;

		public ParameterDetail()
		{
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string DBName
		{
			get
			{
				return ( mDBName );
			}
			set
			{
				mDBName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Type
		{
			get
			{
				return ( mType );
			}
			set
			{
				mType = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object Size
		{
			get
			{
				return ( mSize );
			}
			set
			{
				mSize = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object Scale
		{
			get
			{
				return ( mScale );
			}
			set
			{
				mScale = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public object Required
		{
			get
			{
				return ( mRequired );
			}
			set
			{
				mRequired = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Direction
		{
			get
			{
				return ( mDirection );
			}
			set
			{
				mDirection = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mDBName = DataUtility.GetString( pSource, "dbname" );
			mType = DataUtility.GetString( pSource, "type" );
			mSize = DataUtility.GetInt32( pSource, "size" );
			mScale = DataUtility.GetInt32( pSource, "scale" );
			mRequired = DataUtility.GetBool( pSource, "required" );
			mDirection = DataUtility.GetString( pSource, "direction" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzc:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzc", "method-param", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mDBName ) )
			{
				vNewElement.SetAttribute( "dbname", mDBName );
			}
					
			if ( !String.IsNullOrEmpty( mType ) )
			{
				vNewElement.SetAttribute( "type", mType );
			}
					
			if ( mSize != null )
			{
				if ( mSize is System.Int32 )
				{
					vNewElement.SetAttribute( "size", mSize.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mSize.ToString() + " ] of type [ " + mSize.GetType().ToString() + " ] is invalid.  The field [ Size ] must be null or type [ System.Int32 ]." );
				}
			}
			
			if ( mScale != null )
			{
				if ( mScale is System.Int32 )
				{
					vNewElement.SetAttribute( "scale", mScale.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mScale.ToString() + " ] of type [ " + mScale.GetType().ToString() + " ] is invalid.  The field [ Scale ] must be null or type [ System.Int32 ]." );
				}
			}
			
			if ( mRequired != null )
			{
				if ( mRequired is System.Boolean )
				{
					vNewElement.SetAttribute( "required", mRequired.ToString() );
				}
				else
				{
					throw new ApplicationException( "The value [ " + mRequired.ToString() + " ] of type [ " + mRequired.GetType().ToString() + " ] is invalid.  The field [ Required ] must be null or type [ System.Boolean ]." );
				}
			}
			
			if ( !String.IsNullOrEmpty( mDirection ) )
			{
				vNewElement.SetAttribute( "direction", mDirection );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzc", "description", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.ParameterDetail v_oReturn = new Derived.ParameterDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.DBName = mDBName;
			v_oReturn.Type = mType;
			v_oReturn.Size = mSize;
			v_oReturn.Scale = mScale;
			v_oReturn.Required = mRequired;
			v_oReturn.Direction = mDirection;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections
			
			return ( v_oReturn );
		}

		public virtual Derived.ParameterDetail CloneTyped()
		{
			return ( ( Derived.ParameterDetail )this.Clone() );
		}
   }
}
   